package com.yaolonga.itfc.manager.core;

import com.yaolonga.itfc.manager.core.wifi.WifiSecureType;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/**
 * @Author: yaolong
 * @CreateTime: 2024-10-25  20:53
 * @Description: wifi 工具类
 * @Version: 1.0
 */
public class WifiHelper {


    public static String winWifiProfileTemplate = "<?xml version=\"1.0\"?>\n" +
            "<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\n" +
            "    <name>%s</name>\n" +
            "    <SSIDConfig>\n" +
            "        <SSID>\n" +
            "            <hex>%s</hex>\n" +
            "            <name>%s</name>\n" +
            "        </SSID>\n" +
            "    </SSIDConfig>\n" +
            "    <connectionType>ESS</connectionType>\n" +
            "    <connectionMode>%s</connectionMode>\n" +
            "    <MSM>\n" +
            "        <security>\n" +
            "            <authEncryption>\n" +
            "                <authentication>WPA2PSK</authentication>\n" +
            "                <encryption>AES</encryption>\n" +
            "                <useOneX>false</useOneX>\n" +
            "            </authEncryption>\n" +
            "            <sharedKey>\n" +
            "                <keyType>passPhrase</keyType>\n" +
            "                <protected>false</protected>\n" +
            "                <keyMaterial>%s</keyMaterial>\n" +
            "            </sharedKey>\n" +
            "        </security>\n" +
            "    </MSM>\n" +
            "    <MacRandomization xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v3\">\n" +
            "        <enableRandomization>false</enableRandomization>\n" +
            "        <randomizationSeed>%s</randomizationSeed>\n" +
            "    </MacRandomization>\n" +
            "</WLANProfile>";
    public static String winWifiProfileTemplateUnsafe = "<?xml version=\"1.0\"?>\n" +
            "<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\n" +
            "    <name>%s</name>\n" +
            "    <SSIDConfig>\n" +
            "        <SSID>\n" +
            "            <hex>%s</hex>\n" +
            "            <name>%s</name>\n" +
            "        </SSID>\n" +
            "    </SSIDConfig>\n" +
            "    <connectionType>ESS</connectionType>\n" +
            "    <connectionMode>%s</connectionMode>\n" +
            "    <MSM>\n" +
            "        <security>\n" +
            "            <authEncryption>\n" +
            "                <authentication>open</authentication>\n" +
            "                <encryption>none</encryption>\n" +
            "                <useOneX>false</useOneX>\n" +
            "            </authEncryption>\n" +
            "        </security>\n" +
            "    </MSM>\n%s" +
            "    <MacRandomization xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v3\">\n" +
            "        <enableRandomization>false</enableRandomization>\n" +
            "        <randomizationSeed>%s</randomizationSeed>\n" +
            "    </MacRandomization>\n" +
            "</WLANProfile>";

    public static String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    public static String getWinWifiProfileXml(String profileName,String ssid, String password, boolean isAutoConnect, WifiSecureType secureType) {
        Random random = new Random();
        // 生成10位随机数
        String randomNumber = String.format("%010d", random.nextInt(1000000000));
        String tpl = secureType.name().equals(WifiSecureType.OPEN.name()) ? winWifiProfileTemplateUnsafe : winWifiProfileTemplate;
        return String.format(tpl,
                profileName,
                bytesToHexString(ssid.getBytes()),
                ssid,
                isAutoConnect ? "auto" : "manual",
                password,
                randomNumber
        );
    }

    public static File generateWinWifiProfileFile(String profileName,String ssid, String password, boolean isAutoConnect) {
        String winWifiProfileXml = getWinWifiProfileXml(profileName,ssid, password, isAutoConnect, WifiSecureType.WPA2);
        try {
            // 指定要写入的文件路径和名称
            String filePath = String.format("%s.xml", profileName);
            // 使用 BufferedWriter 和 FileWriter 向文件写入内容
            BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
            writer.write(winWifiProfileXml);
            writer.close();
            return new File(filePath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static File generateWinWifiProfileFile(String profileName,String ssid, boolean isAutoConnect) {
        String winWifiProfileXml = getWinWifiProfileXml(profileName,ssid, "", isAutoConnect, WifiSecureType.OPEN);
        try {
            // 指定要写入的文件路径和名称
            String filePath = String.format("%s.xml", profileName);
            // 使用 BufferedWriter 和 FileWriter 向文件写入内容
            BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
            writer.write(winWifiProfileXml);
            writer.close();
            return new File(filePath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
